package org.kl.wx.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.kl.wx.entity.App;
import org.kl.wx.entity.Groups;
import org.kl.wx.entity.WxUser;
import org.kl.wx.repository.AppDao;
import org.kl.wx.repository.GroupsDao;
import org.kl.wx.repository.WxUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Collections3;

import com.alibaba.fastjson.JSON;

@Component
@Transactional
public class AppService {
	private AppDao appDao;
	private GroupsDao groupsDao;
	private WxUserDao wxUserDao;
	
	public App getApp(String id){
		return appDao.find(App.class, id);
	}
	
	public App getAppByOriginAppId(String originAppId){
		return appDao.findByOriginAppId(originAppId);
	}
	
	public void saveApp(App entity){
		appDao.save(entity);
	}
	
	public void deleteApp(String id){
		appDao.delete(id);
	}
	
	/**
	 * 删除多条记录
	 * 
	 * @param ids
	 */
	/*public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				appDao.delete(id);
				i++;
			}
		}
		return "删除" + i + "条记录";
	}*/
	
	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0, j = 0, z = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
					List<Groups> GList = groupsDao.findByAppId(id);
					List<WxUser> WList = wxUserDao.findByAppId(id);
					j += GList.size();
					z += WList.size();
					groupsDao.delete(GList);
					wxUserDao.delete(WList);
					appDao.delete(id);
					i++; 
			}
		}
		return "删除" + i + "条记录,并且删除" + j + "条分组信息,删除" + z + "条用户相关数据";
	}
	
	/**
	 * 通用查改增删方法
	 * 
	 * @param effectRow
	 */
	public void crud(String inserted, String updated, String deleted) {
		List<App> inserts = JSON.parseArray(inserted, App.class);
		if (Collections3.isNotEmpty(inserts)) {
			for(App a:inserts){
				appDao.save(a);
				
			}
		}
		List<App> updates  =  JSON.parseArray(updated,App.class);
		if (updates.size()>0) {
			for(App b:updates){
				appDao.save(b);
			}	
		}
		List<App> deletes = JSON.parseArray(deleted, App.class);
		if (Collections3.isNotEmpty(deletes)) {
			appDao.deleteInBatch(deletes);
		}
	}
	
	public List<App> getAllApp(){
		return appDao.findAll();
	}
	
	/**
	 * 按条件查询
	 */
	public List<App> getApp(Map<String, Object> searchParams) {
		Specification<App> spec = buildSpecification(searchParams);
		return appDao.findAll(spec);
	}
	
	public List<App> getAllApp(Map<String, Object> searchParams, String sortType, String orderType) {
		Specification<App> spec = KLDynamicSpecifications.buildSpecification(searchParams, App.class);
		return appDao.findAll(spec, CRUDUtils.buildSort(sortType, orderType));
	}
	
	/**
	 * 按条件分页查询
	 */
	public Page<App> getApp(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<App> spec = buildSpecification(searchParams);

		return appDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<App> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<App> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), App.class);
		return spec;
	}
	
	@Autowired
	public void setAppDao(AppDao appDao){
		this.appDao = appDao;
	}

	@Autowired
	public void setGroupsDao(GroupsDao groupsDao) {
		this.groupsDao = groupsDao;
	}

	@Autowired
	public void setWxUserDao(WxUserDao wxUserDao) {
		this.wxUserDao = wxUserDao;
	}
	
	

}
